//
//  HomeItemsCell.m
//  智慧小区
//
//  Created by lab603 on 15/6/29.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "HomeItemsCell.h"
#define itemsCount 6
#define itemBtnWH ([UIScreen mainScreen].bounds.size.width-2*25-2*30)/3
#define padding 25
#define leftPadding 30
#define cellBorder 15
@implementation HomeItemsCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"HomeItemsCell";
    HomeItemsCell *cell = [[HomeItemsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView = [[UIView alloc] init];
        //九宫格功能视图
        for (int i=0; i<itemsCount; i++) {
            UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [itemBtn setTitleColor:APP_THEME_COLOR forState:UIControlStateNormal];
            itemBtn.layer.borderWidth = 1;
            itemBtn.layer.borderColor = [[UIColor lightGrayColor] CGColor];
            [itemBtn.layer setCornerRadius:CORNER_RADIUS_8];
            itemBtn.clipsToBounds = YES;
            itemBtn.titleLabel.font = [UIFont systemFontOfSize:15];
            [itemBtn setBackgroundImage:[UIImage resizedImageWithName:@"ItemBackground"] forState:UIControlStateNormal];
            itemBtn.tag = i;
            [self.contentView addSubview: itemBtn];
        }
    }
    return  self;
}
- (void)setItemsArray:(NSArray *)itemsArray{
    _itemsArray = itemsArray;
    //布局并传递数据
    for (int i = 0 ; i<self.contentView.subviews.count; i++) {
        UIButton *itemBtn = self.contentView.subviews[i];
        if (i<itemsArray.count) {
            itemBtn.hidden = NO;
            //设置数据
            NSString *title = itemsArray[i];
            [itemBtn setTitle:title forState:UIControlStateNormal];
            // 设置子控件的frame
            int maxColumns = (itemsArray.count == 4) ? 2 : 3;
            int col = i % maxColumns;
            int row = i / maxColumns;
            CGFloat sudokuViewX = col * (itemBtnWH + padding)+leftPadding;
            CGFloat sudokuViewY = row * (itemBtnWH + padding)+15;
            itemBtn.frame = CGRectMake(sudokuViewX, sudokuViewY, itemBtnWH, itemBtnWH);
            [itemBtn addTarget:self action:@selector(itemDidClick:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            itemBtn.hidden=YES;
        }
        
    }
}
/**
 *  功能模块被点击
 */
- (void)itemDidClick:(UIButton *)itemBtn{
    IWLog(@"第%ld个被点击",(long)itemBtn.tag);
    if ([self.delegate respondsToSelector:@selector(itemsBtn:didClick:)]) {
        [self.delegate itemsBtn:self didClick:itemBtn];
    }
}
- (void)setFrame:(CGRect)frame
{
    frame.origin.y += cellBorder;
    frame.size.height -= cellBorder;
    [super setFrame:frame];
}
@end
